package com.wn.ziroombackend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.wn.ziroombackend.entity.Room;
import com.wn.ziroombackend.entity.RoomImg;
import com.wn.ziroombackend.entity.RoomInfo;
import com.wn.ziroombackend.service.RoomImgService;
import com.wn.ziroombackend.service.RoomInfoService;
import com.wn.ziroombackend.service.RoomService;
import com.wn.ziroombackend.utils.R;
import com.wn.ziroombackend.utils.RoomCondition;
import com.wn.ziroombackend.vo.RoomInfoVo;
import com.wn.ziroombackend.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.VariableElement;
import javax.swing.plaf.PanelUI;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-27 10:56:29
 */
@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomImgService roomImgService;

    @Autowired
    private RoomInfoService roomInfoService;




    @PostMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer num,
            @RequestParam(defaultValue = "2") Integer size,
            @RequestBody RoomCondition roomCondition
    ) {
        PageInfo<RoomVo> roomList = roomService.getRoomList(num, size, roomCondition.getQuartersName(), roomCondition.getOwnerName());
        return R.ok("ok").data("roomList", roomList);
    }


    @GetMapping("/getImgs/{id}")
    public R getImgs(@PathVariable Integer id) {
        LambdaQueryWrapper<RoomImg> roomImgLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomImgLambdaQueryWrapper.eq(RoomImg::getRoomId, id);

        List<RoomImg> imgList = roomImgService.list(roomImgLambdaQueryWrapper);

        return R.ok("ok").data("imgList", imgList);
    }

    @PostMapping("/add")
    public R add(@RequestBody Room room){
        roomService.save(room);
        return R.ok("添加成功");
    }

    @PutMapping("/edit")
    public R edit(@RequestBody Room room){
        roomService.updateById(room);
        return  R.ok("ok");
    }

    @PutMapping("/editInfo")
    public R editInfo(@RequestBody Room room){
        roomService.updateById(room);
        return  R.ok("ok");
    }


    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Integer id){
      Room room =  roomService.getInfoById(id);
        return R.ok("ok").data("roomInfo", room);
    }


}

